package com.tledu.production.controller.system;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tledu.production.constant.CommonConstant;
import com.tledu.production.core.result.AjaxResult;
import com.tledu.production.model.system.SysRes;
import com.tledu.production.service.system.ISysResService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Antg
 * @date 2021/8/18  14:25
 */
@RestController
@AllArgsConstructor
@RequestMapping("/system/res")
public class SysResController {
    private final ISysResService sysResService;

    @GetMapping("/list")
    public AjaxResult<PageInfo<SysRes>> findAll(Integer pageNum, Integer pageSize) {
        if (pageNum == null) pageNum = CommonConstant.PAGENUM;
        if (pageSize == null) pageSize = CommonConstant.PAGESIZE;
        PageHelper.startPage(pageNum, pageSize);
        List<SysRes> list = sysResService.findAll();
        PageInfo<SysRes> page = PageInfo.of(list);
        return new AjaxResult<>(page);
    }

    @GetMapping("/listShowAll")
    public AjaxResult<SysRes> listAll() {
        List<SysRes> list = sysResService.findAll();
        return new AjaxResult(list);
    }

    @GetMapping("/search")
    public AjaxResult<PageInfo<SysRes>> search(Integer pageNum, Integer pageSize, String type, String searchText) {
        if (pageNum == null) pageNum = 1;
        if (pageSize == null) pageSize = CommonConstant.PAGESIZE;
        PageHelper.startPage(pageNum, pageSize);
        List<SysRes> list = sysResService.search(type, searchText);
        PageInfo<SysRes> page = PageInfo.of(list);
        return new AjaxResult<>(page);
    }

    @GetMapping("/searchShowAll")
    public AjaxResult<SysRes> search(String type, String searchText) {
        List<SysRes> list = sysResService.search(type, searchText);
        return new AjaxResult(list);
    }

    @PostMapping("/add")
    public AjaxResult add(SysRes sysRole) {
        int res = sysResService.add(sysRole);
        if (res == 1) {
            return new AjaxResult("编辑成功");
        } else {
            return new AjaxResult("编辑失败", 500);
        }
    }

    @PostMapping("/edit")
    public AjaxResult edit(SysRes sysRole) {
        int res = sysResService.edit(sysRole);
        if (res == 1) {
            return new AjaxResult("编辑成功");
        } else {
            return new AjaxResult("编辑失败", 500);
        }
    }

    @GetMapping("/del")
    public AjaxResult batchDel(String ids) {
        String[] temp = ids.split(",");
        Integer[] list = new Integer[temp.length];
        for (int i = 0; i < temp.length; i++) {
            list[i] = Integer.parseInt(temp[i]);
        }
        int res = sysResService.del(list);
        if (res >= 1) {
            return new AjaxResult("删除成功");
        } else {
            return new AjaxResult("删除失败", 500);
        }
    }
}
